package com.aracky.assess.controller;

import com.aracky.assess.model.entity.HrKpiUserPerformanceDetail;
import com.aracky.assess.model.entity.HrKpiUserPerformanceHeader;
import com.aracky.assess.model.form.HrKpiUserPerformanceHeaderForm;
import com.aracky.assess.model.form.HrKpiUserPerformanceHeaderUpdateForm;
import com.aracky.assess.model.vo.HrKpiUserPerformanceDetailVO;
import com.aracky.assess.model.vo.HrKpiUserPerformanceHeaderVO;
import com.aracky.assess.model.vo.SysDeptPostName;
import com.aracky.assess.service.HrKpiUserPerformanceDetailService;
import com.aracky.assess.service.HrKpiUserPerformanceHeaderService;
import com.aracky.common.annotation.DataDeal;
import com.aracky.common.util.ComUtil;
import com.aracky.common.util.ConvertUtil;
import com.aracky.common.vo.CommonResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 用户绩效考核明细 前端控制器
 * </p>
 *
 * @author zzp
 * @since 2022-08-01 09:48:11
 */
@RestController
@RequestMapping("/hrKpiUserPerformanceDetail")
@Api(tags = "员工绩效详情接口")
public class HrKpiUserPerformanceDetailController {
    @Resource
    private HrKpiUserPerformanceDetailService hrKpiUserPerformanceDetailService;

    @Resource
    private HrKpiUserPerformanceHeaderService hrKpiUserPerformanceHeaderService;


    @PutMapping("/saveInfo")
    @ApiOperation(value = "保存填写信息")
    public CommonResult saveWriteInfo(@RequestBody HrKpiUserPerformanceHeaderUpdateForm headerUpdate) {
        boolean flag = hrKpiUserPerformanceHeaderService.saveWriteInfo(headerUpdate);
        if (flag) {
            return CommonResult.success("操作成功");
        } else {
            return CommonResult.success("操作失败");
        }
    }


    @PutMapping("/submitInfo")
    @ApiOperation(value = "提交填写信息")
    public CommonResult submitInfo(@RequestBody HrKpiUserPerformanceHeaderUpdateForm headerUpdate) {
        boolean flag = hrKpiUserPerformanceHeaderService.submitInfo(headerUpdate);
        if (flag) {
            return CommonResult.success("操作成功");
        } else {
            return CommonResult.success("操作失败");
        }
    }


    @PostMapping("/save")
    @ApiOperation(value = "启动考核")
    public CommonResult saveInfo(@RequestBody HrKpiUserPerformanceHeaderForm hrKpiUserPerformanceHeaderForm) {
        //根据考核类型和考核周期去查询是否开启过考核
        LambdaQueryWrapper<HrKpiUserPerformanceHeader> lambdaQueryWrapper = new LambdaQueryWrapper<HrKpiUserPerformanceHeader>()
                .eq(HrKpiUserPerformanceHeader::getPerformancePeriod, hrKpiUserPerformanceHeaderForm.getPerformancePeriod())
                .eq(HrKpiUserPerformanceHeader::getPerformanceType, hrKpiUserPerformanceHeaderForm.getPerformanceType());
        List<HrKpiUserPerformanceHeader> list = hrKpiUserPerformanceHeaderService.list(lambdaQueryWrapper);

        if (ComUtil.isNotNullList(list)) {
            return CommonResult.error("当前周期下的考核已开启!");
        }

        //新增权重配置详情
        hrKpiUserPerformanceHeaderService.saveHeader(hrKpiUserPerformanceHeaderForm);
        return CommonResult.success("启动考核成功!");

    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除")
    @ApiImplicitParam(name = "ids", value = "表id")
    @Transactional
    public CommonResult deleteInfo(Long[] ids) {
        List<Long> list = new ArrayList<>();
        for (Long id : ids) {
            list.add(id);
        }
        hrKpiUserPerformanceDetailService.removeBatchByIds(list);
        return CommonResult.success("删除成功!");
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "查看详情")
    @DataDeal(dict = true, user = true)
    public CommonResult<HrKpiUserPerformanceDetailVO> getInfo(@PathVariable Long id) {
        HrKpiUserPerformanceDetailVO hrKpiUserPerformanceDetailVO = hrKpiUserPerformanceDetailService.getDetailInfo(id);
        if (hrKpiUserPerformanceDetailVO == null) {
            return CommonResult.error("查询失败!");
        }
        return CommonResult.success(hrKpiUserPerformanceDetailVO, "查询详情成功!");
    }

    @GetMapping("/postList")
    @ApiOperation(value = "通过用户Id查询Header和Detail数据")
    @ApiImplicitParam(name = "hId", value = "用户id")
    @DataDeal(user = true, dict = true)
    public CommonResult<HrKpiUserPerformanceHeaderVO> getInfoList(Long hId) {
        LambdaQueryWrapper<HrKpiUserPerformanceDetail> lambdaQueryWrapper = new LambdaQueryWrapper<HrKpiUserPerformanceDetail>()
                .eq(HrKpiUserPerformanceDetail::getDelFlag, 0)
                .eq(HrKpiUserPerformanceDetail::getHId, hId);
        List<HrKpiUserPerformanceDetailVO> hrKpiUserPerformanceDetailVOList = ConvertUtil.sourceToTargets(hrKpiUserPerformanceDetailService.list(lambdaQueryWrapper), HrKpiUserPerformanceDetailVO.class);
        //通过用户id查询Header数据
        HrKpiUserPerformanceHeaderVO hrKpiUserPerformanceHeaderVO = hrKpiUserPerformanceHeaderService.getHeaderInfo(hId);
        if (hrKpiUserPerformanceHeaderVO == null) {
            return CommonResult.error("该用户没有参与绩效考核!");
        }
        hrKpiUserPerformanceHeaderVO.setHrKpiUserPerformanceDetailVOList(hrKpiUserPerformanceDetailVOList);
        //根据postId查询对应的部门和岗位
        SysDeptPostName sysDeptPostName = hrKpiUserPerformanceDetailService.getDeptPostNameInfo(hrKpiUserPerformanceHeaderVO.getPostId());
        if (sysDeptPostName != null) {
            hrKpiUserPerformanceHeaderVO.setDeptName(sysDeptPostName.getDeptName());
            hrKpiUserPerformanceHeaderVO.setPostName(sysDeptPostName.getPostName());
        }
        return CommonResult.success(hrKpiUserPerformanceHeaderVO, "查询数据成功!");
    }
}
